﻿Public Class frm_user_list
    Public Sub loadData1()

        Call InitGrid()

        If txt_search.Text <> "" Then
            Db.Where("WHERE nim LIKE '%" & txt_search.Text & "%'")
        End If

        Db.OrderBy("nim", cls_database.sorting.Ascending)
        DataGridView1.DataSource = Connection.ExecuteToDataTable(Db.GetQueryString)

    End Sub

    Public Sub loadData2()

        Call InitGrid()

        If txt_search.Text <> "" Then
            Db.Where("WHERE user LIKE '%" & txt_search.Text & "%'")
        End If

        Db.OrderBy("user", cls_database.sorting.Ascending)
        DataGridView1.DataSource = Connection.ExecuteToDataTable(Db.GetQueryString)

    End Sub

    Public Sub InitGrid()
        ' init grid from database
        ' user
        Db.FlushCache()
        Db.Selects("*")
        Db.From("tbl_user")
        DataGridView1.DataSource = Connection.ExecuteToDataTable(Db.GetQueryString)
    End Sub
    Private Sub cmd_add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_add.Click
        Frm_Manajemen_User_add.Show()
    End Sub

    Private Sub cmd_edit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_edit.Click
        Frm_Manajemen_User_edit.initDataEdit(DataGridView1.SelectedCells(0).Value)
        Frm_Manajemen_User_edit.Show()
    End Sub

    Private Sub frm_user_list_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        InitGrid()
    End Sub
    Private Sub cmd_delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_delete.Click
        Try
            Dim query As String = vbNullString
            If MsgBox("Anda Yakin Menghapus User " + DataGridView1.SelectedCells(0).Value + "?", MsgBoxStyle.YesNo + MsgBoxStyle.Exclamation, "Warning") = MsgBoxResult.Yes Then

                query &= "Delete FROM tbl_user "
                query &= " WHERE user = '" & DataGridView1.SelectedCells(0).Value & "'"

                If Connection.ExecuteNonQuery(query) Then ' jika eksekusi queri berhasil
                    MsgBox("Data Berhasil Dihapus", MsgBoxStyle.Information)
                    InitGrid()
                Else
                    MsgBox("as")
                End If
            End If

        Catch ex As Exception

        End Try
        
    End Sub
    Private Sub DataGridView1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseClick
        cmd_edit.Enabled = True
        cmd_delete.Enabled = True
    End Sub
    Private Sub DataGridView1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDoubleClick
        Frm_Manajemen_User_edit.initDataEdit(DataGridView1.SelectedCells(0).Value)
        Frm_Manajemen_User_edit.Show()
    End Sub

    Private Sub cmd_refresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_refresh.Click
        InitGrid()
    End Sub

    Private Sub cmd_search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_search.Click
        If ComboBox1.SelectedIndex = 0 Then
            Call loadData1()
        ElseIf ComboBox1.SelectedIndex = 1 Then
            Call loadData2()
        Else
            MsgBox("Pilih Kategori Terlebih Dahulu !", MsgBoxStyle.Information, "Information")
        End If
    End Sub
End Class